1 DATA

## Longitudinal data
data_sum <- loadlongitudinaldata(dataset = "DATA_Adults_G1G29.csv", rm_generation1 = 1,rm_generation2 = 7,rm_generation3 = 29)

## Phenotyping steps
data_G0 <- loadfitnessdata(dataset = "Selection_Phenotypage_G0_G7_G8.csv", generation = "G1")
data_G7 <- loadfitnessdata(dataset = "Selection_Phenotypage_G0_G7_G8.csv", generation = "G7")
data_G29 <- loadfitnessdata(dataset = 
                            "PERFORMANCE_Comptage_adultes_G13G14G15G16G17G18G19G20G21G22G23G24G25G26G27G28G29.csv",
                            generation = "29")


#Compute log change
data_logchange<-computelogchange(fitness_dataset_intermediate = data_G7, fitness_dataset_final = data_G29)
#Lines: CE2 and CR2 do not pass Geary's test (the threshold of a standardized mean greater than 3)
data_logchange$Line_type  <- ifelse(data_logchange$Line == "CR2"| data_logchange$Line == "CE2", "solid","dashed")


#Formatting for plotting pairwise
EC_Cran_Cher_G7<-formattingplotpair(data_logchange, "7", "Cherry", "Cranberry")
EC_Straw_Cran_G7<-formattingplotpair(data_logchange, "7", "Cranberry", "Strawberry")
EC_Straw_Cher_G7<-formattingplotpair(data_logchange, "7", "Strawberry", "Cherry")
EC_Cran_Cher_G29<-formattingplotpair(data_logchange, "29", "Cherry", "Cranberry")
EC_Straw_Cran_G29<-formattingplotpair(data_logchange, "29", "Cranberry", "Strawberry")
EC_Straw_Cher_G29<-formattingplotpair(data_logchange, "29", "Strawberry", "Cherry")


#Formatting for plot pool
TEMP_dataG29_CheCran <- formattinglogchange(data_logchange, "29", "Cherry", "Cranberry")
TEMP_dataG29_CranStraw <- formattinglogchange(data_logchange, "29", "Cranberry", "Strawberry")
TEMP_dataG29_StrawChe <- formattinglogchange(data_logchange, "29", "Strawberry", "Cherry")

2 SUP: Pairwise of Fitness difference

2.1 Plot

#Limit axis 
lim_G7<-max(abs(min(data_logchange[data_logchange$Generation=="7",]$logchange-
                      1.96*data_logchange[data_logchange$Generation=="7",]$sd_logchange, na.rm = T)),
            max(data_logchange[data_logchange$Generation=="7",]$logchange+
                  1.96*data_logchange[data_logchange$Generation=="7",]$sd_logchange, na.rm = T))

lim_G29<-max(abs(min(data_logchange[data_logchange$Generation=="29",]$logchange-
                       1.96*data_logchange[data_logchange$Generation=="29",]$sd_logchange, na.rm = T)),
            max(data_logchange[data_logchange$Generation=="29",]$logchange+
                  1.96*data_logchange[data_logchange$Generation=="29",]$sd_logchange, na.rm = T))

#Plot
PAIR_StrawCran_G7<-ggplot(data = EC_Straw_Cran_G7,
                          aes(x = logchange_Strawberry,y = logchange_Cranberry, color=Fruit_s, group = Line)) +
  geom_errorbar(aes(ymin = logchange_Cranberry-1.96*sd_logchange_Cranberry,
                    ymax = logchange_Cranberry+1.96*sd_logchange_Cranberry, linetype=Line_type),
                width=0.02,size=0.2,alpha=1) + 
  geom_errorbarh(aes(xmin = logchange_Strawberry-1.96*sd_logchange_Strawberry,
                     xmax = logchange_Strawberry+1.96*sd_logchange_Strawberry, linetype=Line_type),
                 height=0.02,size=0.2,alpha=1) + 
  geom_point(shape=21, size=3,  fill = "#ffffff", stroke=1.3) + 
  xlab("Fitness difference\nin strawberry")  +     
  ylab("Fitness difference\nin cranberry")  +
  xlim(-lim_G7,lim_G7) + 
  ylim(-lim_G7,lim_G7) +  
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  theme(axis.title.x = element_text(size=10, colour = "#3FAA96"),
        axis.title.y = element_text(size=10, colour = "#FDB424")) +
  theme_LO_sober
 PAIR_StrawCran_G7

PAIR_StrawChe_G7<-ggplot(data = EC_Straw_Cher_G7,
                         aes(x = logchange_Cherry,y = logchange_Strawberry, group=Line, color=Fruit_s)) + 
  geom_errorbar(aes(ymin = logchange_Strawberry-1.96*sd_logchange_Strawberry,
                    ymax = logchange_Strawberry+1.96*sd_logchange_Strawberry, linetype=Line_type),
                  width=0.02, size=0.2, alpha=1) + 
  geom_errorbarh(aes(xmin = logchange_Cherry-1.96*sd_logchange_Cherry,
                     xmax = logchange_Cherry+1.96*sd_logchange_Cherry, linetype=Line_type),
                  height=0.02, size=0.2, alpha=1) + 
  geom_point(shape=21, size=3,  fill = "#ffffff", stroke=1.3) + 
  ylab("Fitness difference\nin strawberry")  + 
  xlab("Fitness difference\nin cherry")  + 
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  xlim(-lim_G7,lim_G7) + 
  ylim(-lim_G7,lim_G7) +    
  theme(axis.title.x = element_text(size=10, colour = "#BC3C6D"),
           axis.title.y = element_text(size=10, colour = "#3FAA96")) +
  theme_LO_sober
 PAIR_StrawChe_G7

PAIR_CheCran_G7<-ggplot(data = EC_Cran_Cher_G7,aes(x = logchange_Cranberry,y = logchange_Cherry, 
                                          group=Line, color=Fruit_s)) + 
  geom_errorbar(aes(ymin = logchange_Cherry-1.96*sd_logchange_Cherry,
                      ymax = logchange_Cherry+1.96*sd_logchange_Cherry, linetype=Line_type),
                  width=0.02, size=0.2, alpha=1) + 
  geom_errorbarh(aes(xmin = logchange_Cranberry-1.96*sd_logchange_Cranberry,
                       xmax = logchange_Cranberry+1.96*sd_logchange_Cranberry, linetype=Line_type),
                  height=0.02, size=0.2, alpha=1) + 
  geom_point(shape=21, size=3,  fill = "#ffffff", stroke=1.3) + 
  ylab("Fitness difference\nin cherry")  + 
  xlab("Fitness difference\nin cranberry")  + 
  xlim(-lim_G7, lim_G7) + 
  ylim(-lim_G7, lim_G7) +     
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  theme_LO_sober + 
  theme(axis.title.x = element_text(colour = "#FDB424"),
        axis.title.y = element_text(colour = "#BC3C6D")) 
 PAIR_CheCran_G7

################## Final phenotyping step
#Plot
PAIR_CranStraw_G29<-ggplot(data = EC_Straw_Cran_G29,
                          aes(x = logchange_Strawberry, y = logchange_Cranberry,  color=Fruit_s,group=Line)) + 
  geom_errorbar(aes(ymin = logchange_Cranberry-1.96*sd_logchange_Cranberry,
                      ymax = logchange_Cranberry+1.96*sd_logchange_Cranberry, linetype=Line_type),
                  width=0.02,size=0.2,alpha=1) + 
  geom_errorbarh(aes(xmin = logchange_Strawberry-1.96*sd_logchange_Strawberry,
                       xmax = logchange_Strawberry+1.96*sd_logchange_Strawberry, linetype=Line_type),
                  height=0.02,size=0.2,alpha=1) + 
  geom_point(aes(fill = Fruit_s),shape=21, size=3) + 
  xlab("Fitness difference\nin strawberry")  +     
  ylab("Fitness difference\nin cranberry")  +
  xlim(-lim_G29,lim_G29) + 
  ylim(-lim_G29,lim_G29) +     
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  scale_fill_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  theme(axis.title.x = element_text(size=10, colour = "#3FAA96"),
        axis.title.y = element_text(size=10, colour = "#FDB424")) +
  theme_LO_sober
 PAIR_CranStraw_G29

PAIR_StrawChe_G29<-ggplot(data = EC_Straw_Cher_G29,
                         aes(x = logchange_Cherry,y = logchange_Strawberry, group=Line , color=Fruit_s)) + 
  geom_errorbar(aes(ymin = logchange_Strawberry-1.96*sd_logchange_Strawberry,
                      ymax = logchange_Strawberry+1.96*sd_logchange_Strawberry, linetype=Line_type),
                  width=0.02,size=0.2,alpha=1) + 
  geom_errorbarh(aes(xmin = logchange_Cherry-1.96*sd_logchange_Cherry,
                       xmax = logchange_Cherry+1.96*sd_logchange_Cherry, linetype=Line_type),
                  height=0.02,size=0.2,alpha=1) + 
  geom_point(aes(fill = Fruit_s),shape=21, size=3) + 
  ylab("Fitness difference\nin strawberry")  + 
  xlab("Fitness difference\nin cherry")  + 
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  scale_fill_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  xlim(-lim_G29,lim_G29) + 
  ylim(-lim_G29,lim_G29) +       
  theme(axis.title.x = element_text(colour = "#BC3C6D"),
           axis.title.y = element_text(colour = "#3FAA96")) +
  theme_LO_sober
 PAIR_StrawChe_G29

PAIR_CheCran_G29<-ggplot(data = EC_Cran_Cher_G29,
                         aes(x = logchange_Cranberry,y = logchange_Cherry,group=Line, color=Fruit_s)) + 
  geom_errorbar(aes(ymin = logchange_Cherry-1.96*sd_logchange_Cherry,
                      ymax = logchange_Cherry+1.96*sd_logchange_Cherry, linetype=Line_type),
                  width=0.02,size=0.2,alpha=1) + 
  geom_errorbarh(aes(xmin = logchange_Cranberry-1.96*sd_logchange_Cranberry,
                       xmax = logchange_Cranberry+1.96*sd_logchange_Cranberry, linetype=Line_type),
                  height=0.02,size=0.2,alpha=1) + 
  geom_point(aes(fill = Fruit_s), shape=21, size=3) + 
  ylab("Fitness difference\nin cherry")  + 
  xlab("Fitness difference\nin cranberry")  + 
  xlim(-lim_G29,lim_G29) + 
  ylim(-lim_G29,lim_G29) +   
  scale_fill_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  theme_LO_sober + 
  theme(axis.title.x = element_text(colour = "#FDB424"),
        axis.title.y = element_text(colour = "#BC3C6D")) 
 PAIR_CheCran_G29

 #Legend
TEMP_data_logchange_sum_Straw_Cher_G7<-data_logchange
TEMP_data_logchange_sum_Straw_Cher_G7$Pheno<-sample(c("Intermediate", "Final"),
                                                    length(data_logchange$logchange), 
                                                    replace=TRUE)

PAIR_plot_legend<- ggplot(data=TEMP_data_logchange_sum_Straw_Cher_G7, 
                          aes(x=Treatment, y=logchange, group=Fruit_s, colour=Fruit_s,shape=Pheno)) +
  geom_errorbar(aes(ymin=logchange-1.96*sd_logchange, ymax=logchange+1.96*sd_logchange),
                  width=0.01,size=1) +
  geom_point(size=3, stroke=1.3) + 
  scale_shape_manual(name = "Phenotyping step", 
                     labels = c("Intermediate", "Final"),values=c(21,16)) +
  scale_color_manual(name= "Selection fruit", 
                     values=c("#BC3C6D", "#FDB424", "#3FAA96"), 
                     label=c("Cherry", "Cranberry", "Strawberry")) +   
  theme_LO_sober
PAIR_legend<-lemon::g_legend(PAIR_plot_legend)

rm(TEMP_data_logchange_sum_Straw_Cher_G7)





PAIR_Fig_Sup <-  cowplot::ggdraw() +
  cowplot::draw_plot(PAIR_CheCran_G7+theme(legend.position = "none"), 
            x =0.00, y = 0.5, width = 0.22, height =0.45) +
  cowplot::draw_plot(PAIR_StrawCran_G7+theme(legend.position = "none"), 
            x = 0.26, y = 0.5, width = 0.22, height = 0.45) +
  cowplot::draw_plot(PAIR_StrawChe_G7+theme(legend.position = "none"), 
            x = 0.52, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(PAIR_legend, x = 0.80, y = 0.5, width = 0.1, height = 0.1) +
  cowplot::draw_plot(PAIR_CheCran_G29+theme(legend.position = "none"), 
            x =0.00, y = 0, width = 0.22, height = 0.45) +
  cowplot::draw_plot(PAIR_CranStraw_G29+theme(legend.position = "none"), 
            x = 0.26, y = 0, width = 0.22, height = 0.45) +
  cowplot::draw_plot(PAIR_StrawChe_G29+theme(legend.position = "none"), 
            x = 0.52, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot_label(c("Intermediate phenotyping step", "A", "B", "C", " ",
                    "Final phenotyping step", "D", "E", "F", " "),  
                  x = c(0.26,0,0.26,0.52,0.82,0.2,0,0.26,0.52,0.82), 
                  y = c(1.01,0.97,0.97,0.97,0.97,0.5,0.47, 0.47, 0.47, 0.47), 
                  hjust = c(-0.25,-0.25,-0.25,-0.25,-0.25,-0.75,-0.75,-0.75,-0.75,-0.75), 
                  vjust = c(1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5),
                  size = 14) 
 PAIR_Fig_Sup

cowplot::save_plot(file=here::here("figures", "SUPMAT_LogChange_Pairwise.pdf"), PAIR_Fig_Sup, 
                   base_height = 15/cm(1), base_width = 30/cm(1), dpi = 610)

3 SUP: Long vs Fitness

3.1 Data

#To consider fruit maternal effect and GF maternal effect: new plot 
## Y-axis = log change = log(Fitness pheno)-log(fitness initial)
## X-axis = fitness temp / fitness initial
 ## example: for CEA during first phase 
    ## log(mean (CE1 from G2 to G5) / mean (Cherry G2))

##To check with Nico: for x-axis: denominator=mean Cherry G2 or mean CE1 G2 (???????????)


####### Phenotyping steps 
#Compute log change
data_logchange <- computelogchange(fitness_dataset_intermediate = data_G7, fitness_dataset_final = data_G29)

#Remove allopatric data
data_fitness_pheno <- data_logchange[data_logchange$SA==1,]
data_fitness_pheno <- data_fitness_pheno[, c("Line", "Fruit_s", "logchange", "sd_logchange")]


####### Longitudinal data 
#Compute log change
data_logchange_long <- computelogchange_forlongdata(longitudinal_dataset = data_sum)


####### Merge data
data_compare_longitudinal_pheno <- merge(data_logchange_long,data_fitness_pheno,by=c("Line", "Fruit_s"))
data_compare_longitudinal_pheno <- droplevels(data_compare_longitudinal_pheno)

## Does not seem to be a correlation between measures
cor.test(data_compare_longitudinal_pheno$logchange[data_compare_longitudinal_pheno$Phase=="first_prepool"], data_compare_longitudinal_pheno$logchange_long[data_compare_longitudinal_pheno$Phase=="first_prepool"])
## 
##  Pearson's product-moment correlation
## 
## data:  data_compare_longitudinal_pheno$logchange[data_compare_longitudinal_pheno$Phase == "first_prepool"] and data_compare_longitudinal_pheno$logchange_long[data_compare_longitudinal_pheno$Phase == "first_prepool"]
## t = -0.37623, df = 12, p-value = 0.7133
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.6039532  0.4482880
## sample estimates:
##        cor 
## -0.1079732
cor.test(data_compare_longitudinal_pheno$logchange[data_compare_longitudinal_pheno$Phase=="second_postpool"], data_compare_longitudinal_pheno$logchange_long[data_compare_longitudinal_pheno$Phase=="second_postpool"])
## 
##  Pearson's product-moment correlation
## 
## data:  data_compare_longitudinal_pheno$logchange[data_compare_longitudinal_pheno$Phase == "second_postpool"] and data_compare_longitudinal_pheno$logchange_long[data_compare_longitudinal_pheno$Phase == "second_postpool"]
## t = 0.91427, df = 9, p-value = 0.3844
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.3737080  0.7587141
## sample estimates:
##       cor 
## 0.2915193
####### Geary test
#Lines: CE2 and CR2 do not pass Geary's test (the threshold of a standardized mean greater than 3)
data_compare_longitudinal_pheno$Line_type  <- ifelse(data_compare_longitudinal_pheno$Line == "CR2"|
                                                       data_compare_longitudinal_pheno$Line == "CE2", "solid","dashed")

3.2 PLOT Step=f(Phase)

PLOT_IntermediatePheno_LOGCHANGE<-ggplot(data=data_compare_longitudinal_pheno[data_compare_longitudinal_pheno$Phase=="first_prepool",], 
       aes(y=logchange,x=logchange_long, color=Fruit_s,shape=Fruit_s, linetype=Line_type)) +
  geom_errorbar(aes(x = logchange_long, 
                    ymin = logchange-(1.96*sd_logchange), 
                    ymax = logchange+(1.96*sd_logchange)),
                  width=0.02,size=0.2,alpha=1) +
  geom_errorbarh(aes(y = logchange, 
                    xmin = logchange_long-1.96*sd_logchange_long,
                    xmax = logchange_long+1.96*sd_logchange_long),
                 height=0.02,size=0.2,alpha=1) +
  geom_point(size=3, fill = "#ffffff", stroke=1.3) +
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_shape_manual(values=c(21, 22, 24)) + 
  xlab("Fitness difference between average fitness\nacross generations 2 to 5 and generation 2") + 
  ylab("Fitness difference between\nintermediate and initial phenotyping steps") + 
  labs(color="Selection fruit", shape="Selection fruit") + 
  geom_abline(intercept = 0, slope = 1, color="black", linetype="dashed", size=0.8) +
  ylim(-1.9,0.9) + 
  xlim(-1.9,0.9) +
  guides(linetype=FALSE) +
  ggtitle("Intermediate phenotyping step vs. Phase 1") +
  theme_LO_sober 
PLOT_IntermediatePheno_LOGCHANGE 

PLOT_FinalPheno_LOGCHANGE <- 
  ggplot(data=data_compare_longitudinal_pheno
                        [data_compare_longitudinal_pheno$Phase=="second_postpool",], 
                        aes(y=logchange, x=logchange_long, 
                            color=Fruit_s, shape=Fruit_s, fill=Fruit_s, linetype=Line_type)) +
  geom_errorbar(aes(x = logchange_long, 
                    ymin = logchange-(1.96*sd_logchange),
                    ymax = logchange+(1.96*sd_logchange)),
                  width=0.02,size=0.2,alpha=1) +
  geom_errorbarh(aes(y = logchange, 
                    xmin = logchange_long-1.96*sd_logchange_long,
                    xmax = logchange_long+1.96*sd_logchange_long),
                 height=0.02,size=0.2,alpha=1) +
  geom_abline(intercept = 0, slope = 1, color="black", 
                 linetype="dashed", size=0.8) +
  geom_point(size=3, stroke=1) +
  scale_fill_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_shape_manual(values=c(21, 22, 24)) + 
  xlab("Fitness difference between average fitness\nacross generations 13 to 26 and generation 2") + 
  ylab("Fitness difference between\nfinal and initial phenotyping steps") + 
  labs(color="Selection fruit", shape="Selection fruit", fill="Selection fruit") + 
  ylim(-0.5,1.7) +
  xlim(-0.5,1.7) +
  guides(linetype=FALSE) +
  ggtitle("Final phenotyping step vs. Phase 3") +
  theme_LO_sober
PLOT_FinalPheno_LOGCHANGE  

PLOT_Comparison_Pheno_Longitudinal_LOGCHANGE <- cowplot::ggdraw() +
  cowplot::draw_plot(PLOT_IntermediatePheno_LOGCHANGE + 
                       theme(axis.title.y = element_text(size=10),
                             axis.title.x = element_text(size=10)),
            x = 0, y = 0.5, width = 1, height = 0.5) +
  cowplot::draw_plot(PLOT_FinalPheno_LOGCHANGE + 
                       theme(axis.title.y = element_text(size=10),
                             axis.title.x = element_text(size=10)),
            x =0, y = 0, width = 1, height = 0.5) +
  cowplot::draw_plot_label(c("A", "B"),  
                  x = c(0,0), y = c(1,0.5), 
                  size = 14) 
PLOT_Comparison_Pheno_Longitudinal_LOGCHANGE

cowplot::save_plot(file=here::here("figures", "SUPMAT_LogChange_Pheno_Longitudinal.pdf"),
                   PLOT_Comparison_Pheno_Longitudinal_LOGCHANGE, base_height = 20/cm(1), base_width = 15/cm(1), dpi = 1200)

3.3 PLOT Step/Phase=f(Phase)

PLOT_IntermediatePheno_TEMP<-ggplot(data=data_compare_longitudinal_pheno[data_compare_longitudinal_pheno$Phase=="first_prepool",], 
       aes(y=logchange/logchange_long,x=logchange_long, color=Fruit_s,shape=Fruit_s)) +
  geom_errorbarh(aes(y = logchange/logchange_long, 
                    xmin = logchange_long-1.96*sd_logchange_long,
                    xmax = logchange_long+1.96*sd_logchange_long, 
                    linetype=Line_type),
                 height=0.02,size=0.2,alpha=1) +
  geom_point(size=3, fill = "#ffffff", stroke=1.3) +
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_shape_manual(values=c(21, 22, 24)) + 
  xlab("Fitness difference between average fitness\nacross generations 2 to 5 and generation 2") + 
  ylab("Fitness difference between phenotyping steps /\nFitness difference between phases") + 
  labs(color="Selection fruit", shape="Selection fruit") + 
  geom_abline(intercept = 0, slope = 1, color="black", linetype="dashed", size=0.8) +
  ylim(-3.9,4.4) +
  xlim(-3.9,4.4) +
  ggtitle("Intermediate phenotyping step vs. Phase 1") +
  theme_LO_sober 
PLOT_IntermediatePheno_TEMP 

PLOT_FinalPheno_TEMP <- ggplot(data=data_compare_longitudinal_pheno
                        [data_compare_longitudinal_pheno$Phase=="second_postpool",], 
                        aes(y=logchange/logchange_long,x=logchange_long, color=Fruit_s,shape=Fruit_s,fill=Fruit_s)) +
  geom_errorbarh(aes(y = logchange/logchange_long, 
                    xmin = logchange_long-1.96*sd_logchange_long,
                    xmax = logchange_long+1.96*sd_logchange_long, 
                    linetype=Line_type),
                 height=0.02,size=0.2,alpha=1) +
  geom_abline(intercept = 0, slope = 1, color="black", 
                 linetype="dashed", size=0.8) +
  geom_point(size=3, stroke=1) +
  scale_fill_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_shape_manual(values=c(21, 22, 24)) + 
  xlab("Fitness difference between average fitness\nacross generations 13 to 26 and generation 2") + 
  ylab("Fitness difference between phenotyping steps /\nFitness difference between phases") + 
  labs(color="Selection fruit", shape="Selection fruit", fill="Selection fruit") + 
  ylim(-0.5,1.7) +
  xlim(-0.5,1.7) +
  ggtitle("Final phenotyping step vs. Phase 3") +
  theme_LO_sober
PLOT_FinalPheno_TEMP  

PLOT_Comparison_TEMP <- cowplot::ggdraw() +
  cowplot::draw_plot(PLOT_IntermediatePheno_TEMP + 
                       theme(axis.title.y = element_text(size=10),
                             axis.title.x = element_text(size=10)),
            x = 0, y = 0.5, width = 1, height = 0.5) +
  cowplot::draw_plot(PLOT_FinalPheno_TEMP + 
                       theme(axis.title.y = element_text(size=10),
                             axis.title.x = element_text(size=10)),
            x =0, y = 0, width = 1, height = 0.5) +
  cowplot::draw_plot_label(c("A", "B"),  
                  x = c(0,0), y = c(1,0.5), 
                  size = 14) 
PLOT_Comparison_TEMP

cowplot::save_plot(file=here::here("figures", "SUPMAT_LogChange_Pheno-div-Long_Longitudinal.pdf"),
                   PLOT_Comparison_TEMP, base_height = 20/cm(1), base_width = 15/cm(1), dpi = 1200)

3.4 PLOT Maternal effects

m <- lm(logchange ~ logchange_long + offset(logchange_long),
        data = data_compare_longitudinal_pheno[data_compare_longitudinal_pheno$Phase=="second_postpool",]) 

# le summary permet directement de vérifier si la pente est significativement différente de 1 à partir de la p-value associée à la variable logchange_long et de tester si l'évolution du changement maternel est significatif (p-value associée à l'intercept)

summary(m)
## 
## Call:
## lm(formula = logchange ~ logchange_long + offset(logchange_long), 
##     data = data_compare_longitudinal_pheno[data_compare_longitudinal_pheno$Phase == 
##         "second_postpool", ])
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.31398 -0.17353 -0.05823  0.12347  0.53029 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)   
## (Intercept)     0.05477    0.21373   0.256  0.80351   
## logchange_long -0.81441    0.20300  -4.012  0.00306 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2743 on 9 degrees of freedom
## Multiple R-squared:  0.08498,    Adjusted R-squared:  -0.01669 
## F-statistic: 0.8359 on 1 and 9 DF,  p-value: 0.3844
#pval of logchange_long = 0.003 => slope is different of 1 
#pval of Intercept = 0.80 => no evolution of maternal effect??? 


## Ratio effet maternels pop dérivée sur effet maternels pop ancestrale??
exp(coef(m)[1]) 
## (Intercept) 
##    1.056301
# proportion of adaptation due to maternal effects
data_compare_longitudinal_pheno$mateff <- (data_compare_longitudinal_pheno$logchange_long -
  data_compare_longitudinal_pheno$logchange) / data_compare_longitudinal_pheno$logchange_long

data_compare_longitudinal_pheno
##    Line    Fruit_s           Phase logchange_long sd_logchange_long   logchange
## 1   CE1     Cherry   first_prepool     0.33816796        0.04332430 -0.27403677
## 2   CE2     Cherry   first_prepool     0.14436267        0.05963021 -0.56171884
## 3   CE3     Cherry   first_prepool     0.46747518        0.05873193  0.18528246
## 4   CE4     Cherry   first_prepool     0.39817852        0.03355308  0.12791340
## 5   CEA     Cherry second_postpool     1.57245790        0.02955303  0.14767000
## 6   CEB     Cherry second_postpool     1.55711969        0.02872255  0.25038029
## 7   CEC     Cherry second_postpool     1.61441010        0.02863731  0.35854062
## 8   CR1  Cranberry   first_prepool    -0.41891064        0.06108829  0.26983737
## 9   CR2  Cranberry   first_prepool    -0.44425078        0.02665804 -0.10684877
## 10  CR3  Cranberry   first_prepool    -0.08878235        0.03934015  0.05809882
## 11  CR4  Cranberry   first_prepool    -0.31794722        0.05591827  0.35156311
## 12  CR5  Cranberry   first_prepool    -0.44134580        0.07287785  0.18840336
## 13  CRA  Cranberry second_postpool     0.49072243        0.02497960 -0.14885350
## 14  CRB  Cranberry second_postpool     0.60455161        0.02677848  0.29719689
## 15  CRC  Cranberry second_postpool     0.64866188        0.02340626  0.02703717
## 16  CRD  Cranberry second_postpool     0.55460135        0.02652983 -0.15627744
## 17  CRE  Cranberry second_postpool     0.75471935        0.02312381  0.72513486
## 18  FR1 Strawberry   first_prepool     0.39149042        0.03843131  0.33051044
## 19  FR2 Strawberry   first_prepool    -0.24506580        0.06386743  0.09022737
## 20  FR3 Strawberry   first_prepool     0.08495270        0.03464317  0.13335674
## 21  FR4 Strawberry   first_prepool     0.13293173        0.03338454  0.58063316
## 22  FR5 Strawberry   first_prepool    -0.41701639        0.03195222  0.18650420
## 23  FRA Strawberry second_postpool     0.86504109        0.03421319  0.15709032
## 24  FRB Strawberry second_postpool     0.96698510        0.03291146  0.56023100
## 25  FRC Strawberry second_postpool     1.05024613        0.03374030  0.36640738
##    sd_logchange Line_type      mateff
## 1    0.25731181    dashed  1.81035697
## 2    0.57349396     solid  4.89102546
## 3    0.11827433    dashed  0.60365285
## 4    0.24311831    dashed  0.67875364
## 5    0.15742840    dashed  0.90608970
## 6    0.10098643    dashed  0.83920293
## 7    0.11692997    dashed  0.77791231
## 8    0.21318308    dashed  1.64414066
## 9    0.46435229     solid  0.75948546
## 10   0.13676999    dashed  1.65439607
## 11   0.12714934    dashed  2.10572789
## 12   0.14497197    dashed  1.42688378
## 13   0.11935520    dashed  1.30333542
## 14   0.15311973    dashed  0.50840113
## 15   0.11563812    dashed  0.95831855
## 16   0.11566948    dashed  1.28178337
## 17   0.12754300    dashed  0.03919933
## 18   0.08222221    dashed  0.15576367
## 19   0.17892718    dashed  1.36817612
## 20   0.16884574    dashed -0.56977628
## 21   0.13295026    dashed -3.36790492
## 22   0.25058617    dashed  1.44723471
## 23   0.14115599    dashed  0.81840132
## 24   0.12842826    dashed  0.42064154
## 25   0.12011698    dashed  0.65112237
#Plot
PLOT_Maternaleffects<-ggplot(data=data_compare_longitudinal_pheno, 
       aes(y=mateff,x=logchange_long, color=Fruit_s, shape=Fruit_s, fill=interaction(Phase,Fruit_s))) +
  geom_point(size=3,  stroke=1.3) +
  scale_fill_manual(name = "Phase", 
                      values = c("#ffffff","#BC3C6D", "#ffffff","#FDB424", "#ffffff", "#3FAA96"), 
                      breaks = c("first_prepool.Cherry", "second_postpool.Cherry"), 
                      labels = c("Phase 1","Phase 3")) +
  scale_color_manual(values=c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_shape_manual(values=c(21, 22, 24)) + 
  xlab("Fitness difference between average fitness\nacross generations 2 to 5 (13 to 26) and generation 2") + 
  ylab("Proportion of adaptation\ndue to evolution of maternal effects") + 
  labs(color="Selection fruit", shape="Selection fruit") + 
  guides(fill = guide_legend(override.aes = list(fill = c("white","black"), shape=21))) +
  theme_LO_sober 
PLOT_Maternaleffects 

cowplot::save_plot(file=here::here("figures", "SUPMAT_Maternal_effects.pdf"),
                   PLOT_Maternaleffects, base_height = 20/cm(1), base_width = 15/cm(1), dpi = 1200)

4 SUP: Adaptation 8fruits

4.1 Longitudinal data

data_sum_allfruits <- loadlongitudinaldata_allfruits(dataset = "DATA_Adults_G1G29.csv", rm_generation_max = 5)
data_sum_allfruits_AN<-data_sum_allfruits[data_sum_allfruits$Generation!=1,]

4.2 Analysis

#Models
mod_all_interaction <- lme4::lmer(fitness ~ Fruit_s*Generation + (1|Generation) + (1|Generation:Fruit_s), 
            weights = N, data = data_sum_allfruits_AN)

mod_all_fruit_generation<- lme4::lmer(fitness ~ Fruit_s+Generation + (1|Generation) + (1|Generation:Fruit_s) , 
            weights = N, data = data_sum_allfruits_AN)

mod_all_fruit<- lme4::lmer(fitness ~ Fruit_s + (1|Generation) + (1|Generation:Fruit_s) , 
            weights = N, data = data_sum_allfruits_AN)

mod_all_null<- lme4::lmer(fitness ~ 1  + (1|Generation) + (1|Generation:Fruit_s), 
            weights = N, data = data_sum_allfruits_AN)

MuMIn::model.sel(mod_all_interaction,mod_all_fruit_generation,mod_all_fruit,mod_all_null)
## Model selection table 
##                            (Int) Frt_s      Gnr Frt_s:Gnr             family df
## mod_all_fruit            -2.4510     +                    gaussian(identity) 11
## mod_all_fruit_generation -2.4100     + -0.02122           gaussian(identity) 12
## mod_all_interaction      -1.2580     + -0.56070         + gaussian(identity) 19
## mod_all_null             -0.9595                          gaussian(identity)  4
##                            logLik  AICc delta weight
## mod_all_fruit             -98.116 221.0  0.00  0.942
## mod_all_fruit_generation  -99.726 226.8  5.76  0.053
## mod_all_interaction       -92.293 231.3 10.31  0.005
## mod_all_null             -115.017 238.4 17.42  0.000
## Models ranked by AICc(x) 
## Random terms (all models): 
## '1 | Generation', '1 | Generation:Fruit_s'
#Posthoc
emmeans::emmeans(mod_all_fruit, list(pairwise ~ Fruit_s), adjust = "tukey") #
## $`emmeans of Fruit_s`
##  Fruit_s      emmean    SE  df lower.CL upper.CL
##  Blackcurrant -2.451 0.286 356   -3.237  -1.6654
##  Cherry       -0.528 0.168 423   -0.987  -0.0679
##  Cranberry    -0.483 0.155 487   -0.907  -0.0595
##  Fig          -0.963 0.196 347   -1.501  -0.4253
##  Grape        -2.437 0.357 272   -3.419  -1.4553
##  Rosehips     -2.076 0.285 367   -2.859  -1.2934
##  Strawberry   -0.424 0.160 426   -0.864   0.0148
##  Tomato       -1.505 0.338 149   -2.441  -0.5692
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 8 estimates 
## 
## $`pairwise differences of Fruit_s`
##  contrast                  estimate    SE  df t.ratio p.value
##  Blackcurrant - Cherry      -1.9237 0.328 348 -5.857  <.0001 
##  Blackcurrant - Cranberry   -1.9681 0.323 361 -6.102  <.0001 
##  Blackcurrant - Fig         -1.4882 0.342 321 -4.346  0.0005 
##  Blackcurrant - Grape       -0.0141 0.448 261 -0.032  1.0000 
##  Blackcurrant - Rosehips    -0.3750 0.397 313 -0.945  0.9813 
##  Blackcurrant - Strawberry  -2.0267 0.325 350 -6.235  <.0001 
##  Blackcurrant - Tomato      -0.9460 0.433 181 -2.185  0.3658 
##  Cherry - Cranberry         -0.0444 0.226 381 -0.196  1.0000 
##  Cherry - Fig                0.4355 0.255 329  1.709  0.6813 
##  Cherry - Grape              1.9096 0.391 282  4.884  <.0001 
##  Cherry - Rosehips           1.5487 0.327 349  4.733  0.0001 
##  Cherry - Strawberry        -0.1030 0.229 358 -0.449  0.9998 
##  Cherry - Tomato             0.9777 0.373 167  2.624  0.1547 
##  Cranberry - Fig             0.4799 0.247 349  1.943  0.5222 
##  Cranberry - Grape           1.9539 0.386 287  5.060  <.0001 
##  Cranberry - Rosehips        1.5931 0.321 364  4.957  <.0001 
##  Cranberry - Strawberry     -0.0587 0.221 383 -0.266  1.0000 
##  Cranberry - Tomato          1.0220 0.368 169  2.780  0.1072 
##  Fig - Grape                 1.4741 0.403 271  3.662  0.0072 
##  Fig - Rosehips              1.1132 0.341 319  3.263  0.0265 
##  Fig - Strawberry           -0.5386 0.250 332 -2.152  0.3841 
##  Fig - Tomato                0.5421 0.384 164  1.411  0.8509 
##  Grape - Rosehips           -0.3609 0.448 267 -0.806  0.9927 
##  Grape - Strawberry         -2.0126 0.388 283 -5.184  <.0001 
##  Grape - Tomato             -0.9319 0.480 175 -1.943  0.5235 
##  Rosehips - Strawberry      -1.6518 0.324 353 -5.101  <.0001 
##  Rosehips - Tomato          -0.5711 0.432 180 -1.322  0.8896 
##  Strawberry - Tomato         1.0807 0.370 167  2.923  0.0745 
## 
## Degrees-of-freedom method: kenward-roger 
## P value adjustment: tukey method for comparing a family of 8 estimates
mod_all_fruit<- lme4::lmer(fitness ~ Fruit_s -1+ (1|Generation) + (1|Generation:Fruit_s) , 
            weights = N, data = data_sum_allfruits)
summary(mod_all_fruit)
## Linear mixed model fit by REML ['lmerMod']
## Formula: fitness ~ Fruit_s - 1 + (1 | Generation) + (1 | Generation:Fruit_s)
##    Data: data_sum_allfruits
## Weights: N
## 
## REML criterion at convergence: 258.3
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.7627 -0.6555  0.1107  0.5521  2.8168 
## 
## Random effects:
##  Groups             Name        Variance Std.Dev.
##  Generation:Fruit_s (Intercept) 0.1504   0.3879  
##  Generation         (Intercept) 0.1323   0.3637  
##  Residual                       2.1944   1.4813  
## Number of obs: 147, groups:  Generation:Fruit_s, 33; Generation, 5
## 
## Fixed effects:
##                     Estimate Std. Error t value
## Fruit_sBlackcurrant  -1.7039     0.3351  -5.084
## Fruit_sCherry        -0.3605     0.2572  -1.402
## Fruit_sCranberry     -0.3561     0.2522  -1.412
## Fruit_sFig           -0.8134     0.2799  -2.906
## Fruit_sGrape         -1.9430     0.3575  -5.435
## Fruit_sRosehips      -1.5403     0.3276  -4.701
## Fruit_sStrawberry    -0.2591     0.2540  -1.020
## Fruit_sTomato        -1.9056     0.3355  -5.680
## 
## Correlation of Fixed Effects:
##             Frt_sB Frt_sCh Frt_sCr Frt_sF Frt_sG Frt_sR Frt_sS
## Frut_sChrry 0.318                                             
## Frt_sCrnbrr 0.320  0.409                                      
## Fruit_sFig  0.316  0.375   0.382                              
## Fruit_sGrap 0.285  0.299   0.300   0.297                      
## Frut_sRshps 0.298  0.324   0.327   0.322  0.285               
## Frt_sStrwbr 0.319  0.407   0.414   0.378  0.299  0.325        
## Fruit_sTomt 0.288  0.316   0.319   0.314  0.275  0.290  0.318

4.3 Plot

pd <- position_dodge(0.3) # move them .05 to the left and right


PLOT_ALLFRUITS<-ggplot(data=data_sum_allfruits, 
                            aes(x=factor(Generation),group=Line, y=fitness, colour=Fruit_s)) + 
  facet_wrap(~ Fruit_s, nrow = 8) +
  geom_errorbar(aes(ymin=fitness-1.96*se_fitness, ymax=fitness+1.96*se_fitness),
                width=.1,position=pd,size=0.2,color="black") +
  geom_line(size=0.3,position=pd) +
  geom_point(size = 2, position = pd, shape = 21, fill = "white",stroke = 1.2) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  theme_LO_sober
PLOT_ALLFRUITS

#Extract slope and intercept
dat_8fruits <- expand.grid(Generation=as.numeric(levels(as.factor(data_sum_allfruits_AN$Generation))),
                           Fruit_s=unique(data_sum_allfruits_AN$Fruit_s))
dat_8fruits$fitness_predicted <- predict(mod_all_fruit, newdata = dat_8fruits, 
                          re.form= NA, type = "response")





PLOT_CHERRY <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Cherry",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Cherry",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white",stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  ylab("Fitness") + 
  xlab("Generation") + 
  xlim("1","2","3","4","5") +
  scale_color_manual(values = c("#BC3C6D")) + 
  ggtitle("Cherry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#BC3C6D"))
PLOT_CHERRY

PLOT_CRANBERRY <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Cranberry",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Cranberry",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white",stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  ylab("Fitness") + 
  xlim("1","2","3","4","5") +
  xlab("Generation") + 
  scale_color_manual(values = c("#FDB424")) + 
  ggtitle("Cranberry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#FDB424"))
PLOT_CRANBERRY

PLOT_STRAWBERRY <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Strawberry",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Strawberry",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white",stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  xlim("1","2","3","4","5") +
  ylab("Fitness") + 
  xlab("Generation") + 
  scale_color_manual(values = c("#3FAA96")) + 
  ggtitle("Strawberry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#3FAA96"))
PLOT_STRAWBERRY

PLOT_GRAPE <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Grape",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Grape",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white",stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  xlim("1","2","3","4","5") +
  ylab("Fitness") + 
  xlab("Generation") + 
  scale_color_manual(values = c("#7B7554")) + 
  ggtitle("Grape") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#7B7554"))
PLOT_GRAPE

PLOT_ROSEHIPS <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Rosehips",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Rosehips",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white",stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  xlim("1","2","3","4","5") +
  ylab("Fitness") + 
  xlab("Generation") + 
  scale_color_manual(values = c("#8ACDEA")) + 
  ggtitle("Rose hips") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#8ACDEA"))
PLOT_ROSEHIPS

PLOT_TOMATO <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Tomato",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Tomato",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white", stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  xlim("1","2","3","4","5") +
  ylab("Fitness") + 
  xlab("Generation") + 
  scale_color_manual(values = c( "#ED6F47")) + 
  ggtitle("Tomato") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#ED6F47"))
PLOT_TOMATO

PLOT_FIG <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Fig",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Fig",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white", stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  xlim("1","2","3","4","5") +
  ylab("Fitness") + 
  xlab("Generation") + 
  scale_color_manual(values = c("#93194F")) + 
  ggtitle("Fig") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#93194F"))
PLOT_FIG

PLOT_BLACKCURRANT <- ggplot(data = data_sum_allfruits[data_sum_allfruits$Fruit_s == "Blackcurrant",], 
                            aes(x = factor(Generation), group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin = fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.4,color = "black") + 
  geom_line(size = 0.5,position = pd) + 
  # geom_line(data = dat_8fruits[dat_8fruits$Fruit_s == "Blackcurrant",], 
  #                    aes(x = factor(Generation), y = fitness_predicted, colour = "black", group=NA), size = 0.7) + 
  geom_point(size = 2, position = pd, shape = 21, fill = "white", stroke = 1.2) + 
  ylim(-5.65, 1.25) +
  xlim("1","2","3","4","5") +
  ylab("Fitness") + 
  xlab("Generation") + 
  scale_color_manual(values = c( "#203753")) + 
  ggtitle("Blackcurrant") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#203753"))
PLOT_BLACKCURRANT

DYNAMIC_EIGHT_JOIN <- cowplot::ggdraw() + 
  cowplot::draw_plot(PLOT_STRAWBERRY, x = 0, y = 0, width = 0.5, height = 0.28) + 
  cowplot::draw_plot(PLOT_GRAPE+ theme(axis.title.x = element_blank(),
                                              axis.text.x = element_blank(),
                                              axis.ticks.x = element_blank(),
                                              axis.line.x = element_blank()),
                     x = 0, y = 0.28, width = 0.5, height = 0.23) + 
  cowplot::draw_plot(PLOT_CRANBERRY+ theme(axis.title.x = element_blank(),
                                              axis.text.x = element_blank(),
                                              axis.ticks.x = element_blank(),
                                              axis.line.x = element_blank()),
                     x = 0, y = 0.53, width = 0.5, height = 0.23) + 
  cowplot::draw_plot(PLOT_BLACKCURRANT+ theme(axis.title.x = element_blank(),
                                              axis.text.x = element_blank(),
                                              axis.ticks.x = element_blank(),
                                              axis.line.x = element_blank()),
                     x = 0, y = 0.77, width = 0.5, height = 0.23) + 
  cowplot::draw_plot(PLOT_TOMATO+ theme(axis.title.y = element_blank()), 
                     x = 0.5, y = 0, width = 0.5, height = 0.28) + 
  cowplot::draw_plot(PLOT_ROSEHIPS+ theme(axis.title.y = element_blank(),
                                              axis.title.x = element_blank(),
                                              axis.text.x = element_blank(),
                                              axis.ticks.x = element_blank(),
                                              axis.line.x = element_blank()),
                     x = 0.5, y = 0.28, width = 0.5, height = 0.23) + 
  cowplot::draw_plot(PLOT_FIG+ theme(axis.title.y = element_blank(),
                                              axis.title.x = element_blank(),
                                              axis.text.x = element_blank(),
                                              axis.ticks.x = element_blank(),
                                              axis.line.x = element_blank()),
                     x = 0.5, y = 0.53, width = 0.5, height = 0.23) + 
  cowplot::draw_plot(PLOT_CHERRY + theme(axis.title.y = element_blank(),
                                              axis.title.x = element_blank(),
                                              axis.text.x = element_blank(),
                                              axis.ticks.x = element_blank(),
                                              axis.line.x = element_blank()),
                     x = 0.5, y = 0.77, width = 0.5, height = 0.23) +
  cowplot::draw_plot_label(c("A", "C",  "E", "G", "B",  "D", "F",  "H"),  
                  x = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5), 
                  y = c(1, 0.75, 0.50, 0.27, 1, 0.75, 0.5, 0.27), 
                  hjust = c(-0.5, -0.5, -0.5,-0.5, -0.5, -0.5, -0.5, -0.5), 
                  vjust = c(1.5, 1.5, 1.5,1.5, 1.5, 1.5,1.5, 1.5),
                  size = 12) 
 DYNAMIC_EIGHT_JOIN

cowplot::save_plot(file =here::here("figures", "SUPMAT_Fitness8Fruits.pdf"), DYNAMIC_EIGHT_JOIN, base_height = 17/cm(1), base_width = 15/cm(1), dpi = 1200)

5 SUP: Pooling effect

5.1 Initial plot from main text

ymin = -50
ymax = 50
 
  
#####################################################################################
##################################      G29        ##################################
#####################################################################################
TEMP_dataG29_CheCran
##    Line Treatment   Fruit_s Generation   logchange sd_logchange SA      Symp
## 14  CEA Cranberry    Cherry         29 -0.21774071   0.13281291  0    Cherry
## 17  CEB Cranberry    Cherry         29 -0.59200204   0.09634940  0    Cherry
## 21  CEC Cranberry    Cherry         29 -0.20670829   0.12591289  0    Cherry
## 37  CRA    Cherry Cranberry         29  0.32537794   0.10025669  0 Cranberry
## 42  CRB    Cherry Cranberry         29  0.16929862   0.12699030  0 Cranberry
## 45  CRC    Cherry Cranberry         29  0.08474123   0.12334598  0 Cranberry
## 47  CRD    Cherry Cranberry         29  0.54736475   0.09718018  0 Cranberry
## 49  CRE    Cherry Cranberry         29 -0.10496044   0.11556266  0 Cranberry
##        Allop Line_type logchange_allop   sd_allop logchange_symp   sd_symp
## 14 Cranberry    dashed     -0.21774071 0.13281291     0.14767000 0.1574284
## 17 Cranberry    dashed     -0.59200204 0.09634940     0.25038029 0.1009864
## 21 Cranberry    dashed     -0.20670829 0.12591289     0.35854062 0.1169300
## 37    Cherry    dashed      0.32537794 0.10025669    -0.14885350 0.1193552
## 42    Cherry    dashed      0.16929862 0.12699030     0.29719689 0.1531197
## 45    Cherry    dashed      0.08474123 0.12334598     0.02703717 0.1156381
## 47    Cherry    dashed      0.54736475 0.09718018    -0.15627744 0.1156695
## 49    Cherry    dashed     -0.10496044 0.11556266     0.72513486 0.1275430
TEMP_dataG29_CranStraw
##    Line  Treatment    Fruit_s Generation   logchange sd_logchange SA       Symp
## 39  CRA Strawberry  Cranberry         29 -0.29731422    0.1098141  0  Cranberry
## 40  CRB Strawberry  Cranberry         29 -0.24539416    0.1387043  0  Cranberry
## 43  CRC Strawberry  Cranberry         29 -0.28950778    0.2012393  0  Cranberry
## 48  CRD Strawberry  Cranberry         29 -1.02555272    0.1902082  0  Cranberry
## 51  CRE Strawberry  Cranberry         29 -0.16810249    0.1398057  0  Cranberry
## 69  FRA  Cranberry Strawberry         29 -0.25158762    0.1712755  0 Strawberry
## 71  FRB  Cranberry Strawberry         29  0.04303751    0.1441119  0 Strawberry
## 74  FRC  Cranberry Strawberry         29  0.52109773    0.1254820  0 Strawberry
##         Allop Line_type logchange_allop  sd_allop logchange_symp   sd_symp
## 39 Strawberry    dashed     -0.29731422 0.1098141    -0.14885350 0.1193552
## 40 Strawberry    dashed     -0.24539416 0.1387043     0.29719689 0.1531197
## 43 Strawberry    dashed     -0.28950778 0.2012393     0.02703717 0.1156381
## 48 Strawberry    dashed     -1.02555272 0.1902082    -0.15627744 0.1156695
## 51 Strawberry    dashed     -0.16810249 0.1398057     0.72513486 0.1275430
## 69  Cranberry    dashed     -0.25158762 0.1712755     0.15709032 0.1411560
## 71  Cranberry    dashed      0.04303751 0.1441119     0.56023100 0.1284283
## 74  Cranberry    dashed      0.52109773 0.1254820     0.36640738 0.1201170
TEMP_dataG29_StrawChe
##    Line  Treatment    Fruit_s Generation   logchange sd_logchange SA       Symp
## 13  CEA Strawberry     Cherry         29  0.01971359    0.1475176  0     Cherry
## 16  CEB Strawberry     Cherry         29 -0.15850680    0.0983504  0     Cherry
## 19  CEC Strawberry     Cherry         29  0.21131464    0.1237821  0     Cherry
## 68  FRA     Cherry Strawberry         29 -0.50599095    0.1378652  0 Strawberry
## 72  FRB     Cherry Strawberry         29  0.19377147    0.1094819  0 Strawberry
## 75  FRC     Cherry Strawberry         29  0.32151693    0.1140574  0 Strawberry
##         Allop Line_type logchange_allop  sd_allop logchange_symp   sd_symp
## 13 Strawberry    dashed      0.01971359 0.1475176      0.1476700 0.1574284
## 16 Strawberry    dashed     -0.15850680 0.0983504      0.2503803 0.1009864
## 19 Strawberry    dashed      0.21131464 0.1237821      0.3585406 0.1169300
## 68     Cherry    dashed     -0.50599095 0.1378652      0.1570903 0.1411560
## 72     Cherry    dashed      0.19377147 0.1094819      0.5602310 0.1284283
## 75     Cherry    dashed      0.32151693 0.1140574      0.3664074 0.1201170
# Limits
ymin_CheCranG29=min(min(TEMP_dataG29_CheCran$logchange_allop-1.96*TEMP_dataG29_CheCran$sd_allop, na.rm= TRUE),
                min(TEMP_dataG29_CheCran$logchange_symp-1.96*TEMP_dataG29_CheCran$sd_symp, na.rm= TRUE))
ymax_CheCranG29=max(max(TEMP_dataG29_CheCran$logchange_allop + 1.96*TEMP_dataG29_CheCran$sd_allop, na.rm= TRUE),
                max(TEMP_dataG29_CheCran$logchange_symp + 1.96*TEMP_dataG29_CheCran$sd_symp, na.rm= TRUE))


CheCran_G29 <- ggplot(data = TEMP_dataG29_CheCran) + 
  geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
  xlab("Fitness difference in\nselective environment")  + 
  ylab("Fitness difference in\nalternative environment")  + 
  ggtitle("Cherry vs. Cranberry") + 
  labs(shape = "Test fruit", color = "Evolution fruit") + 
  scale_shape_manual(values = c(16, 15)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424"))  + 
  coord_cartesian(ylim = c(ymin_CheCranG29, ymax_CheCranG29), 
                  xlim = c(ymin_CheCranG29, ymax_CheCranG29)) + 
  theme_LO_sober
 CheCran_G29

# Limits
ymin_CranStrawG29=min(min(TEMP_dataG29_CranStraw$logchange_allop-1.96*TEMP_dataG29_CranStraw$sd_allop, na.rm= TRUE),
                min(TEMP_dataG29_CranStraw$logchange_symp-1.96*TEMP_dataG29_CranStraw$sd_symp, na.rm= TRUE))
ymax_CranStrawG29=max(max(TEMP_dataG29_CranStraw$logchange_allop + 1.96*TEMP_dataG29_CranStraw$sd_allop, na.rm= TRUE),
                max(TEMP_dataG29_CranStraw$logchange_symp + 1.96*TEMP_dataG29_CranStraw$sd_symp, na.rm= TRUE))

#Plot
 CranStraw_G29 <- ggplot(data = TEMP_dataG29_CranStraw) + 
   geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
   geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
   geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
   xlab("Fitness difference in\nselective environment")  + 
   ylab("Fitness difference in\nalternative environment")  + 
   ggtitle("Cranberry vs. Strawberry") + 
   coord_cartesian(ylim = c(ymin_CranStrawG29, ymax_CranStrawG29), 
                  xlim = c(ymin_CranStrawG29, ymax_CranStrawG29)) + 
   labs(shape = "Test fruit", color = "Selection fruit") + 
   scale_shape_manual(values = c(15, 17)) + 
   scale_color_manual(values = c("#FDB424", "#3FAA96"))  + 
   theme_LO_sober
 CranStraw_G29

# Limits
ymin_StrawCheG29=min(min(TEMP_dataG29_StrawChe$logchange_allop-1.96*TEMP_dataG29_StrawChe$sd_allop, na.rm= TRUE),
                min(TEMP_dataG29_StrawChe$logchange_symp-1.96*TEMP_dataG29_StrawChe$sd_symp, na.rm= TRUE))
ymax_StrawCheG29=max(max(TEMP_dataG29_StrawChe$logchange_allop + 1.96*TEMP_dataG29_StrawChe$sd_allop, na.rm= TRUE),
                max(TEMP_dataG29_StrawChe$logchange_symp + 1.96*TEMP_dataG29_StrawChe$sd_symp, na.rm= TRUE))


StrawChe_G29 <- ggplot(data = TEMP_dataG29_StrawChe) + 
  geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp),
                  width= 0.02, size = 0.2, alpha =1) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp),
                 height = 0.02, size = 0.2, alpha =1) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
  coord_cartesian(ylim = c(ymin_StrawCheG29, ymax_StrawCheG29), 
                  xlim = c(ymin_StrawCheG29, ymax_StrawCheG29)) + 
    xlab("Fitness difference in\nselective environment")  + 
    ylab("Fitness difference in\nalternative environment")  + 
  ggtitle("Strawberry vs. Cherry") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(16, 17)) + 
  scale_color_manual(values = c("#BC3C6D", "#3FAA96"))  + 
  theme_LO_sober
 StrawChe_G29

5.2 Data

########### Hypothesis 1 
#Hypothesis 1: effect of pool 
#Take the mean of all populations for each selection fruit 

# Mean per fruit
data_adults_sum_hypo1<-Rmisc::summarySE(data_logchange[data_logchange$Generation == "7",],
          measurevar="logchange",
          groupvars=c("Fruit_s","Treatment"))

5.3 Plot

# CheCran_G29 + geom_point(data = data_adults_sum_hypo1[data_adults_sum_hypo1$Fruit_s == c("Cherry","Cranberry"),]
#                            size=1.5, position=pd, fill="white") + 
# 
# CranStraw_G29
# StrawChe_G29
 
 
# legend_tradevide <-  ggplot(data = data_logchange[data_logchange$Generation == "29",],
#                           aes(x =logchange, y = logchange,  color = Symp,fill = Symp, shape = Allop)) + 
#   geom_point(size =2.5, fill = "white") + 
#   labs(shape = "Test fruit", color = "Selection fruit") + 
#   scale_shape_manual(values = c(16, 15, 17)) + 
#   scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
#   theme_LO_sober
# legend_trade <- lemon::g_legend(legend_tradevide)


# SUP_MAT <- cowplot::ggdraw() + 
#   cowplot::draw_plot(CheCran_G7 + theme(legend.position = "none"), 
#             x = 0.01, y = 0.5, width = 0.22, height = 0.45) + 
#   cowplot::draw_plot(CranStraw_G7 + theme(legend.position = "none"), 
#             x = 0.31, y = 0.5, width = 0.22, height = 0.45) + 
#   cowplot::draw_plot(StrawChe_G7 + theme(legend.position = "none"), 
#             x = 0.61, y = 0.5, width = 0.22, height = 0.45) + 
#   cowplot::draw_plot(legend_trade, x = 0.85, y = 0.5, width = 0.1, height = 0.1) + 
#   cowplot::draw_plot(CheCran_G29 + theme(legend.position = "none"), 
#             x = 0.01, y = 0, width = 0.22, height = 0.45) + 
#   cowplot::draw_plot(CranStraw_G29 + theme(legend.position = "none"), 
#             x = 0.31, y = 0, width = 0.22, height = 0.45) + 
#   cowplot::draw_plot(StrawChe_G29 + theme(legend.position = "none"), 
#             x = 0.61, y = 0, width = 0.22, height = 0.45) + 
#   cowplot::draw_plot_label(c("Intermediate phenotyping step", "A", "B", "C", " ",
#                     "Final phenotyping step", "D", "E", "F", " "),  
#                   x = c(0.30, 0.01, 0.30, 0.61, 0.92, 0.250, 0.01, 0.30, 0.61, 0.92), 
#                   y = c(0.99, 0.95, 0.95, 0.95, 0.95, 0.49, 0.45, 0.45, 0.45, 0.45), 
#                   hjust = c(-0.25, -0.25, -0.25, -0.25, -0.25, -0.75, -0.75, -0.75, -0.75, -0.75), 
#                   vjust = c(1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5),
#                   size = 16) 
#  SUP_MAT
# #  
# #  
# # 
# cowplot::save_plot(file =here::here("figures", "SUP_MAT_Correlation.pdf"),
#                    SUP_MAT,
#                    base_height = 18/cm(1), base_width = 34/cm(1), dpi = 610)
# 
#